package com.liuyucheng.idea_springboot.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@Slf4j
@ServerEndpoint(value = "/chat") //调用地址：ws://localhost:9000/SpringbootDemo/chat
@Component
public class ChatEndPoint {

    //建立连接时发送系统广播
    @OnOpen
    public void onOpen(Session session, EndpointConfig config) throws IOException {
        log.info("进入OnOpen");
        //主动给前端发送消息
        session.getBasicRemote().sendText("hello，我是后端，给你发一个消息");
    }

    //用户之间的信息发送
    @OnMessage
    public void onMessage(String message,Session session){
        log.info("进入OnMessage，收到的消息是：{}",message);
    }
    //用户断开连接的断后操作
    @OnClose
    public void onClose(Session session){
        log.info("进入OnClose");
    }
}
